module cl {
	export class RegroupAction extends ActionPerformer {
		constructor() {
			super();
			this.type = 2;
			this.paramKeys = "[]";
			this.paramVals = "[]";
		}

		public accept() : void {
			// let vals = JSON.parse(this.paramVals);
			// let keys = JSON.parse(this.paramKeys);
			let troop = Campaign.troop[this.troopid];
			let tgtTroopid = Campaign.troop_map[this.tid];
			if (tgtTroopid < 0) {
				// 新建一支部队并移动
				let appendix = String(Math.floor(Math.random() * 100));
				let newTroop = new TroopEx(
					troop.name + "_" + appendix,
					troop.force,
					troop.troop_type,
					troop.aliasname + "_" + appendix,
					troop.emplace_time.concat(),
					new Tile(troop.tile.x, troop.tile.y),
					troop.orientation,
					troop.level,
					troop.morale,
					Math.floor(0.5 * troop.strength),
					Math.floor(0.5 * troop.ammo),
					Math.floor(0.5 * troop.fuel),
					troop.radio_silence,
					troop.move_remain
				);
				newTroop.init();

				// 修改各种缓存
				let newTroopid = Campaign.troop.length;
				Campaign.troop.splice(newTroopid, 0, newTroop);
				Campaign.troop_force.push(newTroop.force);
				Campaign.troop_plane.push(newTroop.plane);
				if (newTroop.plane == 0) {
					Campaign.troop_map[newTroop.tid] = newTroopid;
				}
				let forceTroopCache = newTroop.plane == 0 ? Campaign.force[newTroop.force].innerTroop : Campaign.force[newTroop.force].courtTroop;
				forceTroopCache.push(newTroopid);
				let typeTroopCache = Campaign.troop_type[newTroop.troop_type].troop;
				typeTroopCache.push(newTroopid);

				// 原部队状态
				troop.strength = Math.ceil(0.5 * troop.strength);
				troop.ammo = Math.ceil(0.5 * troop.ammo);
				troop.fuel = Math.ceil(0.5 * troop.fuel);

				// 原部队的 move_remain 为 this.move_remain，新部队的 move_remain 为 this.other_m_remain[0]
				troop.move_remain = this.move_remain;
				ActionUtil.setAction(new MoveAction(), newTroopid, this.other_m_remain[0], this.tid).accept();

			} else {
				ActionUtil.setAction(new MoveAction(), this.troopid, this.move_remain, this.tid).accept();

				// 合并到另一支部队并删除之
				let tgtTroop = Campaign.troop[tgtTroopid];
				let strengthSum = troop.strength + tgtTroop.strength;
				let rateSrc = troop.strength / strengthSum;
				let rateTgt = tgtTroop.strength / strengthSum;
				tgtTroop.strength = strengthSum;
				tgtTroop.morale = rateSrc * troop.morale + rateTgt * tgtTroop.morale;
				tgtTroop.ammo = rateSrc * troop.ammo + rateTgt * tgtTroop.ammo;
				tgtTroop.fuel = rateSrc * troop.fuel + rateTgt * tgtTroop.fuel;

				// 目标部队的 move_remain 为 this.other_m_remain[0]
				tgtTroop.move_remain = this.other_m_remain[0];
				// 原部队的 move_remain 为 this.move_remain
			}
		}

		public acceptOnce() : void {
			let troop = Campaign.troop[this.troopid];
			let tgtTroopid = Campaign.troop_map[this.tid];
			if (tgtTroopid < 0) {
				this.accept();
				let newTroopid = Campaign.troop_map[this.tid];

				let direction = Interaction.directions[troop.force];
				direction.regroups.splice(direction.regroups.length, 0, -1);
				let oldStrategy = direction.strategies[this.troopid];
				direction.strategies.splice(direction.strategies.length, 0, oldStrategy);
				let teamid = Interaction.findTeam_troop(troop.force, this.troopid);
				if (teamid >= 0) {
					let troopids = direction.teams[teamid].troopids;
					troopids.splice(troopids.length, 0, newTroopid);
				}

			} else {
				this.accept();
				ActionUtil.setAction(new DieAction(), this.troopid, -100).acceptOnce();
			}
		}

		public duration() : number {
			return 300;
		}

		public play(map : CampaignPage) : void {
			let troop = Campaign.troop[this.troopid];
			let tgtTroopid = Campaign.troop_map[this.tid];
			if (tgtTroopid < 0) {
				// 新建一支部队并移动
				let appendix = String(Math.floor(Math.random() * 100));
				let newTroop = new TroopEx(
					troop.name + "_" + appendix,
					troop.force,
					troop.troop_type,
					troop.aliasname + "_" + appendix,
					troop.emplace_time.concat(),
					new Tile(troop.tile.x, troop.tile.y),
					troop.orientation,
					troop.level,
					troop.morale,
					Math.floor(0.5 * troop.strength),
					Math.floor(0.5 * troop.ammo),
					Math.floor(0.5 * troop.fuel),
					troop.radio_silence,
					troop.move_remain
				);
				newTroop.init();
				let newTroopid = Campaign.troop.length;
				map.troopManager.createTroopUnit(newTroop, newTroopid);

				// 为了 move 有效，需要临时性增加 Campaign.troop_force
				let is_extend_temp = Campaign.troop_force.length == newTroopid;
				if (is_extend_temp) {
					Campaign.troop_force.push(newTroop.force);
				}

				ActionUtil.setAction(new MoveAction(), newTroopid, this.other_m_remain[0], this.tid).play(map);

				if (is_extend_temp) {
					Campaign.troop_force.splice(newTroopid, 1);
				}

				let troopGroup = map.groupTroop.getChildByName(String(this.troopid)) as eui.Group;
				map.troopManager.drawBloodSymbol(troopGroup, Math.ceil(0.5 * troop.strength), troop.morale);
				MoveRemainEffect.play(troopGroup, (troop.move_remain+troop.move) / troop.move);

			} else {
				ActionUtil.setAction(new MoveAction(), this.troopid, this.move_remain, this.tid).play(map);

				let troopGroup = map.groupTroop.getChildByName(String(this.troopid)) as eui.Group;
				troopGroup.parent.removeChild(troopGroup);

				let tgtTroop = Campaign.troop[tgtTroopid];
				let strengthSum = troop.strength + tgtTroop.strength;
				let rateSrc = troop.strength / strengthSum;
				let rateTgt = tgtTroop.strength / strengthSum;
				let morale = rateSrc * troop.morale + rateTgt * tgtTroop.morale;

				let tgtTroopGroup = map.groupTroop.getChildByName(String(tgtTroopid)) as eui.Group;

				map.troopManager.drawBloodSymbol(tgtTroopGroup, strengthSum, morale);
				MoveRemainEffect.play(tgtTroopGroup, (this.other_m_remain[0]+tgtTroop.move) / tgtTroop.move);
			}
		}
	}
}